

$(function(){

	$("#form-message").validate({
		rules: {
			name: {
				required: true
			},
			title: {
				required: true,
				maxlength: 500
			},
			email: {
				email: true
			},
			captcha: {
				required: true,
				minlength: 4,
				maxlength: 4
			}
		},
		messages: {
			name: {
				required: "请输入姓名"
			},
			title: {
				required: "请输入留言内容",
				maxlength: "最多可以输入500个字符"
			},
			email: {
				email: "邮箱格式非法"
			},
			captcha: {
				required: "请输入验证码",
				minlength: "验证码最少需要输入4个字符",
				maxlength: "验证码最多可以输入4个字符"
			}
		},
		
		
		submitHandler: function(form){
			
			var eSubmit = document.getElementById("submit");
			eSubmit.disabled = true;
		
			$("#message-msg").removeClass().html(g_sLoadingImage + "发送中...");
			
			$.ajax({
				type: "POST",
				url: "./?controller=message&task=ajax_save",
				data: $(form).serialize(),
				dataType: "json",
				success: function(json){
					
					eSubmit.disabled = false;
					
					if(json.status=="0")
					{
						$(form)[0].reset();
						$("#message-msg").attr("class", "message-notice").html( json.description );
						setTimeout( function(){window.location.reload();}, 300);
					}
					else
					{
						$("#message-msg").attr("class", "message-error").html( json.description );
					}
				}
			});	

		}
	});
});




function saveReply(iMessageID)
{
	var sReply = jQuery("#textarea-reply-"+iMessageID).val();
	if(sReply=='') return;
	
	jQuery("#reply-body-"+iMessageID).html(g_sLoadingImage+"保存中...");
	
	$.ajax({
		type: "POST",
		url: "./?controller=message&task=ajax_reply",
		data:{
			"message_id":iMessageID,
			'reply':sReply
		},
		dataType: "json",
		success: function(json){
			if(json.status=="0")
			{
				jQuery("#reply-title-"+iMessageID).html("您的回复");
				jQuery("#reply-body-"+iMessageID).html(sReply);
			}
			else
			{
				alert( json.description );
			}
		}
	});	
	
	
}



function deleteMessage(iMessageID)
{
	if(confirm("本删作不可恢复，确认要删除吗?"))
	{
		jQuery("#li-"+iMessageID).html(g_sLoadingImage+"删除中...");
		$.ajax({
			type: "GET",
			url: "./?controller=message&task=ajax_delete&message_id="+iMessageID,
			dataType: "json",
			success: function(json){
				if(json.status=="0")
				{
					jQuery("#li-"+iMessageID).remove();
				}
				else
				{
					alert( json.description );
				}
			}
		});	
	}	
}


function deleteReply(iMessageID)
{
	if(confirm("本删作不可恢复，确认要删除吗?"))
	{
		jQuery("#reply-"+iMessageID).html(g_sLoadingImage+"删除中...");
		$.ajax({
			type: "GET",
			url: "./?controller=message&task=ajax_delete_reply&message_id="+iMessageID,
			dataType: "json",
			success: function(json){
				if(json.status=="0")
				{
					jQuery("#reply-"+iMessageID).remove();
				}
				else
				{
					alert( json.description );
				}
			}
		});	
	}
}





